using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
namespace HB.Expand
{
    public static class String2Object
    {
        /// <summary>
        /// 尝试转化base64
        /// </summary>
        /// <param name="s"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static bool TryToBytesFromBase64String(this string s, out byte[] bytes)
        {
            bool b = false;
            bytes = null;
            try
            {
                bytes = Convert.FromBase64String(s);
                b = true;
            }
            catch (Exception ex) { }

            return b;
        }
        /// <summary>
        /// 将字符串转为字节数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] ToBytesFromBase64String(this string s)
        {
            return Convert.FromBase64String(s);
        }
        /// <summary>
        /// 是否base64字符串
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsBase64String(this string s)
        {
            bool b = false;
            try
            {
                var data = Convert.FromBase64String(s);
                b = true;
            }
            catch (Exception ex) { }

            return b;
        }
        /// <summary>
        /// 判定是否符合指定的正则表达式
        /// </summary>
        /// <param name="s"></param>
        /// <param name="patten"></param>
        /// <returns></returns>
        public static bool IsRegexMatch(this string s, string patten)
        {
            if (s == null) { return false; }
            return Regex.IsMatch(s, patten);
        }
        /// <summary>
        /// 判定是否合法的window文件名
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsValidWindowFileName(this string fileName)
        {
            return fileName.IsRegexMatch("^[^/\\\\:\\*\\?\\<\\>\\|\"]{1,255}$");
        }
        /// <summary>
        /// 获取所有匹配的字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="patten"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static List<string> GetRegexMatchList(this string s, string patten, bool ignoreCase = true)
        {
            Regex rgx = new Regex(patten, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
            MatchCollection matches = rgx.Matches(s);
            List<string> lsresult = new List<string>();
            foreach (var str in matches)
            {
                lsresult.Add(str.ToString());
            }
            return lsresult;
        }

        /// <summary>
        /// 获取字符的字节数组,default=utf-8
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encoding">default=utf-8</param>
        /// <returns></returns>
        public static byte[] ToBytes(this string str, Encoding encoding = null)
        {
            if (str == null) { return new byte[0]; }
            if (encoding == null) { encoding = Encoding.UTF8; }
            return encoding.GetBytes(str);
        }


        /// <summary>
        /// 尝试转换指定的日期格式
        /// </summary>
        /// <param name="date"></param>
        /// <param name="Format">格式</param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static bool TryToDateTime(this string date, string Format, out DateTime time)
        {
            time = DateTime.Now;
            bool result = false;
            try
            {
                time = DateTime.ParseExact(date, Format, CultureInfo.CurrentCulture);
                return true;
            }
            catch { }
            return result;
        }
        /// <summary>
        /// 直接转为datetime
        /// </summary>
        /// <param name="date"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string date, string format)
        {
            return DateTime.ParseExact(date, format, CultureInfo.CurrentCulture);
        }
        /// <summary>
        /// 显示限定长度的字符串，超出部分将使用省略号替代
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strMaxLen">最大的字符串长度</param>
        /// <param name="otherChar">替代符号</param>
        /// <returns></returns>
        public static string LimitLen(this string str, int strMaxLen, string otherChar = "...")
        {
            if (str != null)
            {
                if (str.Length > strMaxLen)
                {
                    return $"{str.Substring(0, strMaxLen)}{otherChar }";
                }
                else
                {
                    return str;
                }
            }
            return str;
        }
        /// <summary>
        /// 从url参数字符串中分离参数
        /// </summary>
        /// <param name="urlParamter"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetParamterFromUrlFormat(this string urlParamter)
        {
            var keyValuePairs = new Dictionary<string, string>();
            if (urlParamter.IsNoNullOrEmpty())
            {
                var kvs = urlParamter.Split('&');
                foreach (var kvstr in kvs)
                {
                    var kv = kvstr.Split('=');
                    if (kv.Length == 2)
                    {
                        keyValuePairs[kv[0]] = kv[1];
                    }
                }
            }
            return keyValuePairs;
        }
        
    }
}
